package dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import model.Persistent;

public class AbstractDao<T extends Persistent> {
	
	@PersistenceContext
	public EntityManager em;
	
	public EntityManager getEm() {
		return em;
	}

	public void setEm(EntityManager em) {
		this.em = em;
	}
	
	@Transactional
	public void persist(T object) {
		getEm().persist(object);
		getEm().flush();
	}
	
	@Transactional
	public T merge(T object) {
		return getEm().merge(object);
	}
	
	@Transactional
	public void remove(T object) {
		T attachedObject = getEm().merge(object);
		getEm().remove(attachedObject);
	}

}
